package com.supermap.business.agriculture;

import com.alibaba.dubbo.config.annotation.Reference;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.agrinfo.FarmingService;
import com.supermap.dubbo.api.agrinfo.entity.Farming;
import com.supermap.dubbo.api.agrinfo.entity.FarmingType;
import com.supermap.dubbo.api.common.CropService;
import com.supermap.dubbo.api.common.entity.Crop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:langhongshuai
 * @Description:农事接口层
 * @Date:18/1/8 上午11:26
 */
@Controller
@RequestMapping("FarmingController")
public class FarmingController {
    /**
     * 使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(FarmingController.class);

    @Reference
    private FarmingService farmingService;


    /**
     * 增加农事
     *
     * @param farming
     * @return
     */
    @RequestMapping("/addFarming")
    @ResponseBody
    public JSONUtil addFarming(@RequestBody Farming farming) {
        return farmingService.addFarming(farming);
    }

    /**
     * 根据条件获取农事
     *
     * @param farming
     * @return
     */
    @RequestMapping("/getAllFarming")
    @ResponseBody
    public JSONUtil getAllFarming(@RequestBody Farming farming) {
        return farmingService.getAllFarming(farming);
    }


    /**
     * 更新农事信息
     *
     * @param farming
     * @return
     */
    @RequestMapping("/updateFarming")
    @ResponseBody
    public JSONUtil updateFarming(@RequestBody Farming farming) {
        return farmingService.updateFarming(farming);
    }

    /**
     * 根据农事id删除农事
     *
     * @param farmingid
     * @return
     */
    @RequestMapping("/deleteFarmingById")
    @ResponseBody
    public JSONUtil deleteFarmingById(String farmingid) {
        return farmingService.deleteFarmingById(farmingid);
    }


    /**
     * 当levels为1时，获取农事种类大类，当levels为2时，获取具体农事种类，当levels为0时，获取所有农事种类
     */
    @RequestMapping("/getAllFarmingType")
    @ResponseBody
    public JSONUtil getAllFarmingType(HttpServletRequest request) {
        String levels = request.getParameter("levels");
        return farmingService.getAllFarmingType(Integer.parseInt(levels));
    }

    /**
     * 保存农事种类
     */
    @RequestMapping("/saveFarmingType")
    @ResponseBody
    public JSONUtil saveFarmingType(HttpServletRequest request) {
        String id = request.getParameter("id");
        String parentid = request.getParameter("parentid");
        String code = request.getParameter("code");
        String name = request.getParameter("name");
        String levels = request.getParameter("level");
        String description = request.getParameter("description");
        String imgico = request.getParameter("imgIcon");
        FarmingType farmingType = new FarmingType();
        farmingType.setId(Integer.parseInt(id));
        farmingType.setCode(code);
        farmingType.setParentid(Integer.parseInt(parentid));
        farmingType.setName(name);
        farmingType.setLevel(Integer.parseInt(levels));
        farmingType.setDescription(description);
        farmingType.setImgIco(imgico);
        return farmingService.saveFarmingType(farmingType);
    }

    /**
     * 更新农事种类
     */
    @RequestMapping("updateFarmingType")
    @ResponseBody
    public JSONUtil updateFarmingType(HttpServletRequest request) {
        String id = request.getParameter("id");
        String parentid = request.getParameter("parentid");
        String code = request.getParameter("code");
        String name = request.getParameter("name");
        String levels = request.getParameter("levels");
        String description = request.getParameter("description");
        String imgIco = request.getParameter("imgIcon");
        FarmingType farmingType = new FarmingType();
        farmingType.setId(Integer.parseInt(id));
        farmingType.setCode(code);
        farmingType.setParentid(Integer.parseInt(parentid));
        farmingType.setName(name);
        farmingType.setLevel(Integer.parseInt(levels));
        farmingType.setDescription(description);
        farmingType.setImgIco(imgIco);
        return farmingService.updateFarmingType(farmingType);
    }

    /**
     * 删除农事种类
     */
    @RequestMapping("deleteFarmingType")
    @ResponseBody
    public JSONUtil deleteFarmingType(HttpServletRequest request) {
        String id = request.getParameter("id");
        return farmingService.deleteFarmingType(id);
    }

    /***
     * 获取溯源二维码
     * */
    @RequestMapping("getSourceQRcode")
    @ResponseBody
    public JSONUtil getSourceQRcode(String loginName) {
        return farmingService.getSourceQRcode(loginName);
    }
}
